package dabs.engine.plant;

import dabs.engine.control.ComponentCommand;
import dabs.engine.environment.*;
import dabs.engine.messaging.Notification;
import dabs.engine.messaging.NotificationHandler;

public class Reactor extends Converter implements AcceptsModifier{
	private static final long serialVersionUID = 9103067283645337784L;
	
	//private int powerOutputed;
	
	public Reactor(NotificationHandler mailBox, String name, int rodLevel, int capacity, int outRate) {
		super(mailBox, name, rodLevel, capacity, outRate);
		setConversionRate(rodLevel);
	}
	
	public int getRodLevel() { return getConversionRate(); }
	//public int getPowerOutputed() { return powerOutputed; }
	
	public void setRodLevel(int rodLevel) { 
		if (isDeactivated() == false){ 
			setConversionRate(rodLevel);
			passMessage(new Notification(getName(), "Rods set to " + rodLevel + "%."));
		}
		else { 
			passMessage(new Notification("Reactor", 
					"Rod's have stalled."));
		}
	}
	
	@Override
	protected void inactivatedModifier(Modifier modifier) {
		super.inactivatedModifier(modifier);
		if (modifier instanceof TemporaryDisable) {
			passMessage(new Notification(getName(), "is now active again"));
		}
	}
	
	public void convert() {
		getNewHydroState().convert(getConversionRate(), false);
	}
	
	@Override
	public void tick() {
		getNewHydroState().sendToNext(getHydroProp().getOutRate(), false, getOutputs(), getPressure());
		if (getConversionRate() > 0 && getNewHydroState().getWaterLevel() == 0){
			setHealth(getHealth() - getConversionRate() / 10);
		}
		super.tick();
	}

	@Override
	public void runCommand(ComponentCommand command) {
		assert(command.getDestination().equals(getName()));
		try{
			int newLevel = Integer.parseInt(command.getArguments().get(0)); //Line to be changed depending on arguments.
			if (newLevel <= 100 && newLevel >= 0){
				setRodLevel(newLevel);
			}
			else{
				passMessage(new Notification(getName(), "Rods must be set to a value between 0 and 100"));
			}
		}
		catch (java.lang.NumberFormatException e){
			passMessage(new Notification(getName(), "Rods must be set to an integer value."));
			return;
		}
	}
	
	@Override
	public String read() {
		return String.format("%s\tRod Level: %d\n", super.read(), getRodLevel());
	}
	
}
